var userId = getUrlParam('userId');
layui.config({
    base: ctx + 'static/lib/layui_exts/dtree/' //配置 layui 第三方扩展组件存放的基础目录
}).extend({
    dtree: 'dtree' //定义该组件模块名
}).use(['element','layer','table','dtree'],  function () {
    var dtree = layui.dtree
        ,form = layui.form;
    layer = layui.layer;
    axios.all([axios.get(ctx + 'sys/user/info/' + userId), axios.get(ctx + 'sys/role/list')]).then(axios.spread(function (userRes, roleRes) {
        var userInfo = userRes.data.user;
        var roleList = roleRes.data;
        roleList.data = parseChecked(userInfo.roleIdList, roleList.data);
        var roleTree = [];
        roleList.data.forEach(function (item) {
            roleTree.push({
                id: item.roleId,
                parentId: 0,
                title: item.roleName,
                checkArr: [{ type: 0, checked: item.checkArr[0].checked }]
            })
        });
        dtree.render({
            elem: "#user-role-tree",  //绑定元素
            data: roleTree,
            checkbar: true,
            async: false,
            initLevel: 1
        });
        $('.wrapper').html(
              '<div class="layui-form-item">'
             +  '<label class="layui-form-label">*用户名</label>'
             +  '<div class="layui-input-block">'
             +      '<input type="text" name="username" autocomplete="off" placeholder="用户名" class="layui-input" value="' + userInfo.username + '">'
             +  '</div>'
             +  '</div>'
             +'<div class="layui-form-item">'
             +  '<label class="layui-form-label">*UUID(统一认证32位码)</label>'
             +  '<div class="layui-input-block">'
             +      '<input type="text" name="uuid" autocomplete="off" placeholder="UUID" class="layui-input" value="' + userInfo.uuid + '">'
             +  '</div>'
             +'</div>'
             +'<div class="layui-form-item">'
             +  '<label class="layui-form-label">*真实姓名</label>'
             +  '<div class="layui-input-block">'
             +      '<input type="text" name="realName" autocomplete="off" placeholder="真实姓名" class="layui-input" value="' + userInfo.realName + '">'
             +  '</div>'
             +'</div>'
             +'<div class="layui-form-item">'
             +  '<label class="layui-form-label">*密码</label>'
             +  '<div class="layui-input-block">'
             +      '<input type="text" name="password" autocomplete="off" placeholder="密码" class="layui-input">'
             +  '</div>'
             +'</div>'
             +'<div class="layui-form-item">'
             +  '<label class="layui-form-label">状态</label>'
             +  '<div class="layui-input-block">'
             +      '<input type="checkbox" checked="" name="status" lay-skin="switch" lay-filter="switchTest" lay-text="正常|禁用" value="' + (userInfo.status == 1 ? 'on' : 'off') +'">'
             +  '</div>'
             +'</div>'
        );
        form.render();
    })).catch(function (err) {
        console.log(err)
    });
    form.on('submit(add)', function (data) {
        console.log(data.field);
        var params = dtree.getCheckbarNodesParam("user-role-tree");
        var ids = _.map(params, function (item) {
            return item.nodeId;
        });
        data.field['userId'] = userId;
        data.field['roleIdList'] = ids;
        data.field['status'] === 'on' ?  data.field['status'] = 1 :  data.field['status'] = 0
        axios.post(ctx + 'sys/user/update', data.field).then(function (res) {
            if(res.data.code == 0) {
                layer.msg('保存成功');
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
                parent.table.reload();
            }else {
                layer.msg('保存失败');
            }
        }).catch(function (err) {
            console.log(err);
        });
        return false;
    });
});

$('#cancel').click(function () {
    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index);
});

function parseChecked(checkId, roleList) {
    for(var i =0; i < roleList.length; i ++) {
        roleList[i].checkArr = [{ type: 0, checked: 0}];
        for(var j = 0; j < checkId.length; j ++) {
            if(roleList[i].roleId === checkId[j]) {
                roleList[i].checkArr[0] = { type: 0, checked: 1};
            }
        }
    }
    console.log(roleList);
    return roleList;
}